<template>
	<view class="container">
		<!-- 蓝色背景区域 -->
		<view class="blue-bg"></view>
		
		<view class="content">
			<!-- 当前定位 -->
			<view class="location-section">
				<text class="section-title">当前定位：</text>
				<view class="current-location">
					<text>焦作市</text>
					<view class="refresh-btn" @click="refreshLocation">
						<image class="refresh-icon" src="/static/3.1-刷新@2x.png" mode="aspectFit"></image>
						<text class="refresh-text">重新定位</text>
					</view>
				</view>
			</view>

			<!-- 城市列表卡片 -->
			<view class="city-card">
				<!-- 热门城市标题 -->
				<text class="card-title">热门城市</text>
				
				<!-- 热门城市网格 -->
				<view class="city-grid">
					<view class="city-item" 
						v-for="(city, index) in hotCities" 
						:key="index"
						@click="selectCity(city)"
						hover-class="item-hover"
					>
						<text>{{city}}</text>
					</view>
				</view>

				<!-- 字母分类列表 -->
				<view class="letter-list">
					<text class="letter">A</text>
					<view class="city-list">
						<view class="city-row" 
							v-for="city in cityList.A" 
							:key="city" 
							@click="selectCity(city)"
							hover-class="item-hover"
						>
							<text>{{city}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import amap from '@/utils/amap.js'
import locationService from '@/utils/location.js'

export default {
	data() {
		return {
			hotCities: [
				'北京市', '北京市', '北京市',
				'南京市', '南京市', '南京市',
				'武汉市', '武汉市', '武汉市',
				'深圳市', '深圳市', '深圳市'
			],
			cityList: {
				'A': ['安庆市', '安阳市', '安顺市', '鞍山市', '安康市']
			}
		}
	},
	methods: {
		async refreshLocation() {
			try {
				const location = await locationService.getCurrentLocation();
				const result = await amap.reverseGeocoder(location);
				const city = result.result.address_component.city;
				
				uni.showToast({
					title: '定位成功',
					icon: 'success'
				});
				
				this.selectCity(city);
			} catch (error) {
				console.error('定位失败:', error);
				uni.showToast({
					title: '定位失败',
					icon: 'none'
				});
			}
		},
		selectCity(city) {
			uni.$emit('citySelected', city)
			uni.navigateBack()
		}
	}
}
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #fff;
	position: relative;
}

.blue-bg {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	height: 360rpx;
	background-color: #2979ff;
}

.content {
	position: relative;
	z-index: 1;
	padding: 32rpx;
}

.location-section {
	margin-top: 20rpx;
	margin-bottom: 48rpx;
}

.section-title {
	font-size: 32rpx;
	color: #fff;
	margin-bottom: 24rpx;
}

.current-location {
	display: flex;
	justify-content: space-between;
	align-items: center;
	color: #fff;
	font-size: 26rpx;
}

.refresh-btn {
	display: flex;
	align-items: center;
}

.refresh-icon {
	width: 28rpx;
	height: 28rpx;
	margin-right: 8rpx;
}

.refresh-text {
	color: #fff;
	font-size: 28rpx;
}

.city-card {
	background: #fff;
	border-radius: 16rpx;
	padding: 32rpx;
}

.card-title {
	font-size: 32rpx;
	color: #333;
	font-weight: 500;
	margin-bottom: 32rpx;
}

.city-grid {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 24rpx;
	margin-bottom: 48rpx;
}

.city-item {
	background: #F5F5F5;
	height: 80rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 40rpx;
	font-size: 28rpx;
	color: #333;
}

.letter {
	font-size: 32rpx;
	color: #333;
	font-weight: 500;
	margin-bottom: 24rpx;
}

.city-list {
	display: flex;
	flex-direction: column;
}

.city-row {
	padding: 28rpx 0;
	font-size: 28rpx;
	color: #333;
	border-bottom: 1rpx solid #EEEEEE;
}

.city-row:last-child {
	border-bottom: none;
}

.item-hover {
	opacity: 0.8;
}
</style>

